# === load relevant libraries
library(data.table)
library(ggplot2)
library(DescTools)
library(plm)
library(lfe)
library(dplyr)
library(sandwich)

rm(list = ls())

# get actual style-level rating changes in June 2002
data = readRDS('input_data/morningstar_style_data.RDS')[, list(yyyymm, category, rating_1)]
data = merge(data[yyyymm == 200207, list(category, rating = rating_1)],
             data[yyyymm == 200206, list(category, rating_1 = rating_1)], by = 'category')
data = data[, list(category, drating = rating - rating_1)]

# merged with predicted rating changes using dec 2001 data
tmp  = readRDS('input_data/predicted_style_rating_change_dec_2001.RDS')
data = merge(data, tmp, by = 'category'); rm(tmp)

# give nicer names for plotting
data = data[order(category)]
data[, nn := c('Large-Blend','Large-Growth','Large-Value','Mid-Blend','Mid-Growth','Mid-Value','Small-Blend','Small-Growth','Small-Value')]
data[, offset_x := c(.1,0,-.1,0,0,0,0,0,-.05)]
data[, offset_y := c(0,.05,0,-.05,.05,.04,.05,.05,.05)]

# output Figure A.9
par(mar = c(5,5,2,2))
xx = c(-.55, .35)
plot( data[, list(predicted_rating_change, drating)], pch = 16, xlim = xx, ylim = xx, xlab = 'Predicted Rating Change in June 2002 based on Dec 2001 data', ylab = 'Actual Rating Change in June 2002', family="Times New Roman")
abline(0,1,col=3,lwd=2,lt=3)
for (i in 1:9){
  text(data[i, predicted_rating_change + offset_x], data[i, drating + offset_y], data[i, nn], family="Times New Roman")
}



